package appx.craft.droid.ui;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import appx.craft.droid.R;
import appx.craft.droid.services.LocationListeningServices;
import appx.craft.droid.services.LocationListeningServices.LocalBinder;
import appx.craft.droid.utils.Const;
import appx.craft.droid.utils.Log;
import appx.craft.droid.utils.Pref;

public class ChooseActivity extends  FragmentActivity implements OnClickListener{
		
	public final String TAG = ChooseActivity.class.getCanonicalName();
		
	Button btn_screen_popup,btn_screen_listing,btn_screen_input;
	/**
	 * Help to determined   the service is live or not 
	 */
	boolean mBound = false;
	LocationListeningServices mobject$LocationService;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choose);
		Log.debug(TAG,"[ onCreate ] ");
		Const.CONTEXT = ChooseActivity.this;
		
		
		btn_screen_popup = (Button)findViewById(R.id.btn_screen_popup);
		btn_screen_popup.setOnClickListener(this);
		btn_screen_listing = (Button)findViewById(R.id.btn_screen_listing);
		btn_screen_listing.setOnClickListener(this);
		btn_screen_input = (Button)findViewById(R.id.btn_screen_input);
		btn_screen_input.setOnClickListener(this);
		
		
		if(Const.INSTALL_ATTEMP_FIRST){
			
			/**
			 * Default value for List Screen Activity 
			 * 
			 */
			
		//Yesterday's day
			
		 Calendar cal1 = Calendar.getInstance();
		 DateFormat dateFormat1 = new SimpleDateFormat("dd MMM,yyyy HH:mm:ss");
		 System.out.println("Today's date is "+ dateFormat1.format(cal1.getTime()));
		 cal1.add(Calendar.DATE, -1);
		 Pref.setValue(Const.DURATION_ITEM+"_"+1,String.valueOf(dateFormat1.format(cal1.getTime()))); 
		 
		// 10 minutes
		 Calendar cal2 = Calendar.getInstance();
		 DateFormat dateFormat2 = new SimpleDateFormat("dd MMM,yyyy HH:mm:ss");
		 System.out.println("Today's date is "+ dateFormat2.format(cal2.getTime()));
		 cal2.add(Calendar.MINUTE, -10);
		 
		Pref.setValue(Const.DURATION_ITEM+"_"+2,String.valueOf(dateFormat2.format(cal2.getTime()))); 
		
		// 1 minutes
		 Calendar cal3 = Calendar.getInstance();
		 DateFormat dateFormat3 = new SimpleDateFormat("dd MMM,yyyy HH:mm:ss");
		 System.out.println("Today's date is "+ dateFormat3.format(cal3.getTime()));
		 cal3.add(Calendar.MINUTE, -1);
		 Pref.setValue(Const.DURATION_ITEM+"_"+3,String.valueOf(dateFormat3.format(cal3.getTime())));   
		
		// 10 dec 2012
		 
		 Calendar cal4 = Calendar.getInstance();
		 DateFormat dateFormat4 = new SimpleDateFormat("dd MMM,yyyy HH:mm:ss a");
		 cal4.add(Calendar.DATE,-4);
		 cal4.add(Calendar.HOUR,-7);
		 cal4.add(Calendar.MINUTE,-26);
		 System.out.println("Today's date is "+ dateFormat4.format(cal4.getTime()));
		 
		 Pref.setValue(Const.DURATION_ITEM+"_"+4,String.valueOf(dateFormat4.format(cal4.getTime()))); 
		
			Const.INSTALL_ATTEMP_FIRST = true;
		}
		
		
	}
	@Override
	protected void onStart() {
		super.onStart();
		Log.debug(TAG,"[ onStart ] ");
		/**
		 * If you want to communicate to service then bind it with your activity 
		 * to listen it make service connection class 
		 */
		Intent intent = new Intent(this, LocationListeningServices.class);
		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
	}
	@Override
	protected void onStop() {
		super.onStop();
		Log.debug(TAG,"[ onStop ] ");
	
	}
	@Override
	protected void onPause() {
		super.onPause();
		Log.debug(TAG,"[ onPause ] ");
	}
	@Override
	protected void onResume() {
		super.onResume();
		Log.debug(TAG,"[ onResume ] ");
		
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.debug(TAG,"[ onDestroy ] ");
		// Unbind from the service
		
		  if (mBound) {
			  mobject$LocationService.removeUpdatesService();
			  unbindService(mConnection);
	          mBound = false;
	      }
		
	}
	@Override
	public void onClick(View v) {
		int btnID = v.getId();
		if(btnID == R.id.btn_screen_popup){
			startActivity(new Intent(getApplicationContext(),HomeScreen.class));
		}else if (btnID == R.id.btn_screen_listing) {
			startActivity(new Intent(getApplicationContext(),ListScreenActivity.class));
		}else if (btnID == R.id.btn_screen_input) {
			startActivity(new Intent(getApplicationContext(),InputScreenActivity.class));
		}
	}
	
	
	   /**
	    *  Defines callback for service binding, passed to bindService() 
	    * */
    private ServiceConnection mConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			
			// We've bound to LocalBinder , cast the IBinder and get Location Service instance
			Log.debug(TAG,"[ onServiceConnected ]");
			
			LocalBinder binder = (LocalBinder) service;
			/**
			 * which in turn return Location Listening service object 
			 */
			mobject$LocationService = binder.getService();
			mBound = true;
						 
			
		}
		/**
    	 * Called when a connection to the Service has been lost
    	 */
		@Override
		public void onServiceDisconnected(ComponentName name) {
			 
			//Now turn is to set mBound to false
			Log.debug(TAG,"[ onServiceDisconnected ]");
			mBound = false;
			
		}
		 
	};
	
	
	
}
